package com.ucw.ucwclient.biz.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ucw.ucwclient.dal.dataobject.Account;
import com.ucw.ucwclient.dal.dataobject.UPlan;
import com.ucw.ucwclient.dal.dataobject.UPlanCollect;
import com.ucw.ucwclient.dal.dataobject.UPlanEnroll;
import com.ucw.ucwclient.dal.mapper.AccountMapper;
import com.ucw.ucwclient.dal.mapper.UPlanCollectMapper;
import com.ucw.ucwclient.dal.mapper.UPlanEnrollMapper;
import com.ucw.ucwclient.dal.mapper.UPlanMapper;
import com.ucw.ucwclient.dal.search.NormalSearch;

@Service
public class UPlanService {

	@Resource
	private UPlanMapper uPlanMapper;

	@Resource
	private UPlanCollectMapper uPlanCollectMapper;

	@Resource
	private UPlanEnrollMapper uPlanEnrollMapper;

	@Resource
	private AccountMapper accountMapper;

	public List<UPlan> getList(NormalSearch search) {
		List<UPlan> list = uPlanMapper.selectByPageSearch(search);
		return list;
	}

	public UPlan getOne(int id) {
		return uPlanMapper.selectByPrimaryKey(id);
	}

	public Map<String, String> reportOne(Integer id) {
		Map<String, String> resp = new HashMap<String, String>();
		UPlan us = new UPlan();
		us.setId(id);
		us.setReport(true);
		int result = uPlanMapper.updateByPrimaryKey(us);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> collectOne(Integer id, Long userId) {
		UPlanCollect uc = new UPlanCollect();
		uc.setCreateTime(new Date());
		uc.setPlanId(id);
		uc.setUserId(userId);
		int result = uPlanCollectMapper.insertSelective(uc);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> cancelCollectOne(Integer id, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();
		int result = uPlanCollectMapper.deleteByPrimaryKey(id, userId);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> enrollOne(Integer id, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();
		UPlanEnroll uPlanEnroll = new UPlanEnroll();
		uPlanEnroll.setCreateTime(new Date());
		uPlanEnroll.setUserId(userId);
		int result = uPlanEnrollMapper.insertSelective(uPlanEnroll);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> cancelEnrollOne(Integer id, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();
		int result = uPlanEnrollMapper.deleteByPrimaryKey(id, userId);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public List<Account> checkOneEnroll(NormalSearch search, Long userId) {
		return accountMapper.selectUSEnrollByPageSearch(search);
	}

	public Map<String, String> addOne(UPlan up, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();
		up.setUserId(userId);
		up.setCreateTime(new Date());
		int result = uPlanMapper.insertSelective(up);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	@Transactional
	public Map<String, String> approve(List<Long> userIds, Integer enrollId, Long userId) {
		int result = 0;
		Map<String, String> resp = new HashMap<String, String>();
		if (userIds != null && userIds.size() > 0) {
			for (Long long1 : userIds) {
				UPlanEnroll ua = new UPlanEnroll();
				ua.setPlanId(enrollId);
				ua.setUserId(long1);
				ua.setEnrollAccept(true);
				result = result + uPlanEnrollMapper.updateByPrimaryKeySelective(ua);
			}
		}
		if (result > 0) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

}
